#ifndef VZCONN_CONN_VZSERVER_H_
#define VZCONN_CONN_VZSERVER_H_

#include "vzconn/base/basicdefines.h"
#include "vzconn/conn/vzconnect.h"

namespace vzconn {

class VzServer : public boost::noncopyable,
  public boost::enable_shared_from_this<VzServer> {
 public:
  typedef boost::shared_ptr<VzServer> Ptr;

  static VzServer::Ptr CreateTcpServer(boost::asio::io_service& io_service,
                                       const std::string addr,
                                       uint16 port);

  static VzServer::Ptr CreateIpcServer(boost::asio::io_service& io_service,
                                       const std::string addr,
                                       uint16 port);

  virtual ~VzServer();

  // Signal a new vz connected
  boost::signals2::signal < void(
    VzServer::Ptr vz_server,
    VzConnect::Ptr vz_connect) >SignalNewConnect;
  // Signal server error
  boost::signals2::signal < void(
    VzServer::Ptr vz_server,
    const boost::system::error_code& err) > SignalVzServerError;

  virtual bool Start() = 0;

  virtual void Stop();

  virtual bool Stoped() const {
    return stop_;
  }

  virtual const std::string bind_addr() const = 0;

  virtual boost::asio::io_service& io_service() {
    return io_service_;
  }
 public:
  virtual void HandleAcceptor(VzConnect::Ptr connect,
                              const boost::system::error_code& err);
  virtual void WaitNextConnect() = 0;
  virtual void HandleStopVzServer() = 0;
 protected:
  //
  explicit VzServer(boost::asio::io_service &io_services);
 protected:
  boost::asio::io_service &io_service_;
  bool stop_;
};

}

#endif //  VZCONN_CONN_VZSERVER_H_
